package 进阶阶段;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java_code.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class demo32C3p0连接池 {
    public static void main(String[] args) throws Exception {
        //第一种方式，直接使用c3p0连接池
//        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//        comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
//        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sys");
//        comboPooledDataSource.setUser("root");
//        comboPooledDataSource.setPassword("");
//
//        //数据源连接池有得到连接的功能，可以反复使用
//        Connection connection = comboPooledDataSource.getConnection();

        //第二种方式，从 c3p0-config.cml 配置文件中读取配置来使用c3p0连接池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        Connection connection = comboPooledDataSource.getConnection();

        //sys的数据库中sys_config表的value的赋值
        String a = "6";

        String sql = "select * from sys_config where value = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1 , a);

        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            String string = resultSet.getString("variable");
            System.out.println(string);
        }

        JDBCUtils.close(resultSet, preparedStatement,connection);
    }
}
